package a66weding.com.jiehuntong.adapter;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.MyTaskBean;
import a66weding.com.jiehuntong.beans.ReleaseTaskBean;
import a66weding.com.jiehuntong.common.utils.DateUtils;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.RegularUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.CircularImage;
import de.hdodenhof.circleimageview.CircleImageView;

import static a66weding.com.jiehuntong.R.color.colorRed4;
import static a66weding.com.jiehuntong.R.color.switch_thumb_disabled_material_dark;


/**
 * Created by admin on 2016/7/7.
 */
public class MyTaskBAdapter extends BaseRecyclerAdapter<MyTaskBAdapter.ViewHolder> {
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private Context context;
    private List<ReleaseTaskBean.DataBean> data;
    public MyTaskBAdapter(Context context, List<ReleaseTaskBean.DataBean> data){
        this.context = context;
        if (data != null) {
            this.data = data;
        }
    }
    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }
    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_activity_mytask, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    //作绑定数据用的;
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position, boolean isItem) {
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView,pos);
                }
            });
        }
        holder.isDesignate.setText("指派");

        if(data!=null){
            holder.circleImageView.setVisibility(View.VISIBLE);
            if (data.get(position).getPicture()!=null) {
                x.image().bind(holder.circleImageView, "" + data.get(position).getPicture(), ImageOptionsUtil.getOptions92_92());
                x.image().bind(holder.circleImageView_head, "" + data.get(position).getPicture(), ImageOptionsUtil.getOptions92_92());
            }
            holder.money.setText("¥"+data.get(position).getBudgetMoney());
            holder.task_myTask.setText(""+data.get(position).getBookTitle());
            if(data.get(position).isIsComplete()){
                holder.isComplete.setText("已完成");
                holder.isComplete.setTextColor(Color.RED);
            }
            holder.data.setText(data.get(position).getBookDate());
            //真实姓名
            String real_name = data.get(position).getNvc_RealName();
            //模拟姓名
            String nvc_name = data.get(position).getNvc_Name();
            if (!TextUtils.isEmpty(real_name)){
                holder.name.setText(real_name);
            }else{
               if (!TextUtils.isEmpty(nvc_name)){
                   holder.name.setText(nvc_name);
               }
            }
//           int lastday  = DateUtils.daysBetween(refFormatNowDate(),""+data.get(position).getBookDate());
//            if (lastday==0){
//                holder.item_activity_my_task_last_day.setText("今天");
//            }else if (lastday<0){
//                holder.item_activity_my_task_last_day.setText("逾期"+Math.abs(lastday)+"天");
//            }else {
//                holder.item_activity_my_task_last_day.setText("距离计划完成日还有"+lastday+"天");
//            }

            String dsy = data.get(position).getDays();
            if (RegularUtil.isValidDate(dsy)){
                holder.item_activity_my_task_last_day.setText(dsy);
            }else{
                holder.item_activity_my_task_last_day.setText("距离计划完成还有"+dsy+"天");
            }
        }
    }
    public void append(List<ReleaseTaskBean.DataBean>  data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }
    @Override
    public int getAdapterItemCount() {
        if (data ==null){
            return 0 ;
        }
        return  data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        //头像的图标
        private ImageView my_task_icon;
        //任务
        private TextView task_myTask;
        //金额
        private TextView money;
        //剩余天数
        private TextView day;
        //名字
        private TextView name;
        //日我
        private TextView data;
        //是否完成任务
        private TextView isComplete;
        //是指派还是来自的
        private TextView  isDesignate;
        //圆形图片
        private CircularImage circleImageView;
        //底部的头像
        private CircularImage circleImageView_head;
        //日子
        private TextView item_activity_my_task_last_day;
        public ViewHolder(View itemView) {
            super(itemView);
            my_task_icon = (ImageView) itemView.findViewById(R.id.item_activity_my_task_circle);
            task_myTask = (TextView) itemView.findViewById(R.id.item_activity_my_task_myTask);
            money = (TextView) itemView.findViewById(R.id.item_activity_my_task_money);
            day = (TextView) itemView.findViewById(R.id.item_activity_my_task__day);
            name = (TextView) itemView.findViewById(R.id.item_activity_my_task_worker_name);
            data = (TextView) itemView.findViewById(R.id.item_activity_my_task_worker_data);
            isComplete = (TextView) itemView.findViewById(R.id.item_activity_my_task_isComplete);
            isDesignate = (TextView) itemView.findViewById(R.id.item_activity_my_task_title);
            circleImageView = (CircularImage) itemView.findViewById(R.id.item_activity_my_task_circle);
            circleImageView_head = (CircularImage) itemView.findViewById(R.id.item_activity_my_task_worker_ic);
            item_activity_my_task_last_day = (TextView) itemView.findViewById(R.id.item_activity_my_task_last_day);
        }
    }

    //获得当前时间
    public String refFormatNowDate() {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String retStrFormatNowDate = sdFormatter.format(nowTime);
        return retStrFormatNowDate;
    }

}
